<template>
<j-modal :title="title" :width="width" :visible="visible" :confirmLoading="confirmLoading" @ok="handleOk" @cancel="handleCancel" cancelText="关闭">
    <a-spin :spinning="confirmLoading">
        <a-form :form="form">
            <a-row>
                <a-col :span="12">
                    <a-form-item v-if="title == '新增'" label="订单号" :required="true" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <j-dict-select-request url="/sales/salesOrder/listWSC" v-model="dataList.orderOdd" type="list" :trigger-change="true" @change="getData" />
                    </a-form-item>
                    <a-form-item v-else label="订单号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.orderOdd" placeholder="请输入订单号"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="生产号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.odd" placeholder="请输入生产号"></a-input>
                    </a-form-item>
                </a-col>
                <a-col v-if="title=='编辑'" :span="12">
                    <a-form-item label="下单日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <j-date placeholder="请输入下单日期" style="width: 100%" v-decorator="['date']" :trigger-change="true" />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="客户" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.customerNumber" placeholder="请输入客户"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="交货日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.deliveryDate" placeholder="请输入交货日期"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item v-if="title=='编辑'" label="颜色" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['color', validatorRules.color]" placeholder="请输入颜色"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item v-show="title=='编辑'" label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['specifications']" placeholder="请输入规格"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item v-if="title=='编辑'" label="实际生成数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input-number v-decorator="['numberGenerated', validatorRules.numberGenerated]" placeholder="请输入实际生成数量" style="width: 100%" />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item v-show="title=='编辑'" label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['note']" placeholder="请输入备注"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item v-show="title=='编辑'" label="是否中止执行" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <j-dict-select-tag type="list" v-model="dataList.isExecution" :trigger-change="true" dictCode="yn" @sendName="getName" />
                    </a-form-item>
                </a-col>
            </a-row>
        </a-form>
    </a-spin>
</j-modal>
</template>

<script>
import {
    httpAction,
    getAction
} from '@/api/manage'
import pick from 'lodash.pick'
import JDate from '@/components/jeecg/JDate'
export default {
    name: 'ProductionTaskListModal',
    components: {
        JDate,
    },
    props: ['odd'],
    data() {
        return {
            form: this.$form.createForm(this),
            title: '操作',
            width: 800,
            visible: false,
            dataList: {
                isExecution: '否',
            },
            model: {},
            labelCol: {
                sm: {
                    span: 7,
                },
            },
            wrapperCol: {
                sm: {
                    span: 16,
                },
            },
            confirmLoading: false,
            validatorRules: {
                color: {
                    rules: [{
                        required: true,
                        message: '请输入颜色!'
                    }]
                },
                numberGenerated: {
                    rules: [{
                        required: true,
                        message: '请输入实际生产数量!'
                    }]
                }
            },
            url: {
                add: '/production/productionTaskList/add',
                edit: '/production/productionTaskList/edit',
            },
        }
    },
    watch: {
        odd() {
            if (this.odd) {
                this.$set(this.dataList, 'odd', this.odd)
            }
        },
    },
    methods: {
        getName(value) {
            this.$set(this.dataList, 'isExecution', value)
        },
        async getData(value) {
            this.$set(this.dataList, 'orderOdd', value)
            let url = '/sales/salesOrder/list'
            let params = {
                odd: value,
            }
            let result = await getAction(url, params)
            let {
                dataList
            } = this
            let {
                client,
                clientId,
                deliveryDate
            } = result.result.records[0]
            this.$set(dataList, 'customerNumber', client)
            this.$set(dataList, 'clientId', clientId)
            this.$set(dataList, 'deliveryDate', deliveryDate)
        },
        add() {
            this.edit({})
        },
        edit(record) {
            this.form.resetFields()
            this.model = Object.assign({}, record)
            let {
                orderOdd,
                customerNumber,
                deliveryDate
            } = record;
            this.dataList = {
                orderOdd,
                deliveryDate,
                customerNumber,
                isExecution: record.isExecution ? record.isExecution : '否',
            }
            this.odd && (!record.orderOdd) ? this.$set(this.dataList, 'odd', this.odd) : this.$set(this.dataList, 'odd', record.odd)
            this.visible = true
            this.$nextTick(() => {
                this.form.setFieldsValue(
                    pick(
                        this.model,
                        'date',
                        'color',
                        'specifications',
                        'numberGenerated',
                        'note'
                    )
                )
            })
        },
        close() {
            this.$emit('close')
            this.visible = false
        },
        handleOk() {
            const that = this
            // 触发表单验证
            if (this.dataList.orderOdd == '' || this.dataList.orderOdd == undefined) {
                this.$message.warning('请选择订单号!')
                return false;
            }
            this.form.validateFields((err, values) => {
                if (!err) {
                    that.confirmLoading = true
                    let httpurl = ''
                    let method = ''
                    let formData = {}
                    if (!this.model.id) {
                        httpurl += this.url.add
                        method = 'post'
                        formData = {
                            orderOdd: this.dataList.orderOdd
                        }
                    } else {
                        httpurl += this.url.edit
                        method = 'put';
                        values = Object.assign(values, this.dataList)
                        formData = Object.assign(this.model, values)
                    }
                    httpAction(httpurl, formData, method)
                        .then((res) => {
                            if (res.success) {
                                that.$message.success(res.message)
                                that.$emit('ok')
                            } else {
                                that.$message.warning(res.message)
                            }
                        })
                        .finally(() => {
                            this.dataList = {
                                isExecution: '否',
                            }
                            that.confirmLoading = false
                            that.close()
                        })
                }
            })
        },
        handleCancel() {
            this.close()
        },
        popupCallback(row) {
            this.form.setFieldsValue(
                pick(row, 'date', 'color', 'specifications', 'numberGenerated', 'note')
            )
        },
    },
}
</script>
